/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.autogenerats;

import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.ModeAplicacio;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Tractament;
import java.util.ArrayList;
import java.util.Collection;
import entity.Abonat;

/**
 *
 * @author David
 */
public class ModeAplicacioJpaController implements Serializable {

    public ModeAplicacioJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ModeAplicacio modeAplicacio) {
        if (modeAplicacio.getTractamentCollection() == null) {
            modeAplicacio.setTractamentCollection(new ArrayList<Tractament>());
        }
        if (modeAplicacio.getAbonatCollection() == null) {
            modeAplicacio.setAbonatCollection(new ArrayList<Abonat>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Tractament> attachedTractamentCollection = new ArrayList<Tractament>();
            for (Tractament tractamentCollectionTractamentToAttach : modeAplicacio.getTractamentCollection()) {
                tractamentCollectionTractamentToAttach = em.getReference(tractamentCollectionTractamentToAttach.getClass(), tractamentCollectionTractamentToAttach.getId());
                attachedTractamentCollection.add(tractamentCollectionTractamentToAttach);
            }
            modeAplicacio.setTractamentCollection(attachedTractamentCollection);
            Collection<Abonat> attachedAbonatCollection = new ArrayList<Abonat>();
            for (Abonat abonatCollectionAbonatToAttach : modeAplicacio.getAbonatCollection()) {
                abonatCollectionAbonatToAttach = em.getReference(abonatCollectionAbonatToAttach.getClass(), abonatCollectionAbonatToAttach.getId());
                attachedAbonatCollection.add(abonatCollectionAbonatToAttach);
            }
            modeAplicacio.setAbonatCollection(attachedAbonatCollection);
            em.persist(modeAplicacio);
            for (Tractament tractamentCollectionTractament : modeAplicacio.getTractamentCollection()) {
                ModeAplicacio oldModeAplicacioIdOfTractamentCollectionTractament = tractamentCollectionTractament.getModeAplicacioId();
                tractamentCollectionTractament.setModeAplicacioId(modeAplicacio);
                tractamentCollectionTractament = em.merge(tractamentCollectionTractament);
                if (oldModeAplicacioIdOfTractamentCollectionTractament != null) {
                    oldModeAplicacioIdOfTractamentCollectionTractament.getTractamentCollection().remove(tractamentCollectionTractament);
                    oldModeAplicacioIdOfTractamentCollectionTractament = em.merge(oldModeAplicacioIdOfTractamentCollectionTractament);
                }
            }
            for (Abonat abonatCollectionAbonat : modeAplicacio.getAbonatCollection()) {
                ModeAplicacio oldModeAplicacioIdOfAbonatCollectionAbonat = abonatCollectionAbonat.getModeAplicacioId();
                abonatCollectionAbonat.setModeAplicacioId(modeAplicacio);
                abonatCollectionAbonat = em.merge(abonatCollectionAbonat);
                if (oldModeAplicacioIdOfAbonatCollectionAbonat != null) {
                    oldModeAplicacioIdOfAbonatCollectionAbonat.getAbonatCollection().remove(abonatCollectionAbonat);
                    oldModeAplicacioIdOfAbonatCollectionAbonat = em.merge(oldModeAplicacioIdOfAbonatCollectionAbonat);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ModeAplicacio modeAplicacio) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ModeAplicacio persistentModeAplicacio = em.find(ModeAplicacio.class, modeAplicacio.getId());
            Collection<Tractament> tractamentCollectionOld = persistentModeAplicacio.getTractamentCollection();
            Collection<Tractament> tractamentCollectionNew = modeAplicacio.getTractamentCollection();
            Collection<Abonat> abonatCollectionOld = persistentModeAplicacio.getAbonatCollection();
            Collection<Abonat> abonatCollectionNew = modeAplicacio.getAbonatCollection();
            List<String> illegalOrphanMessages = null;
            for (Tractament tractamentCollectionOldTractament : tractamentCollectionOld) {
                if (!tractamentCollectionNew.contains(tractamentCollectionOldTractament)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Tractament " + tractamentCollectionOldTractament + " since its modeAplicacioId field is not nullable.");
                }
            }
            for (Abonat abonatCollectionOldAbonat : abonatCollectionOld) {
                if (!abonatCollectionNew.contains(abonatCollectionOldAbonat)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Abonat " + abonatCollectionOldAbonat + " since its modeAplicacioId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Tractament> attachedTractamentCollectionNew = new ArrayList<Tractament>();
            for (Tractament tractamentCollectionNewTractamentToAttach : tractamentCollectionNew) {
                tractamentCollectionNewTractamentToAttach = em.getReference(tractamentCollectionNewTractamentToAttach.getClass(), tractamentCollectionNewTractamentToAttach.getId());
                attachedTractamentCollectionNew.add(tractamentCollectionNewTractamentToAttach);
            }
            tractamentCollectionNew = attachedTractamentCollectionNew;
            modeAplicacio.setTractamentCollection(tractamentCollectionNew);
            Collection<Abonat> attachedAbonatCollectionNew = new ArrayList<Abonat>();
            for (Abonat abonatCollectionNewAbonatToAttach : abonatCollectionNew) {
                abonatCollectionNewAbonatToAttach = em.getReference(abonatCollectionNewAbonatToAttach.getClass(), abonatCollectionNewAbonatToAttach.getId());
                attachedAbonatCollectionNew.add(abonatCollectionNewAbonatToAttach);
            }
            abonatCollectionNew = attachedAbonatCollectionNew;
            modeAplicacio.setAbonatCollection(abonatCollectionNew);
            modeAplicacio = em.merge(modeAplicacio);
            for (Tractament tractamentCollectionNewTractament : tractamentCollectionNew) {
                if (!tractamentCollectionOld.contains(tractamentCollectionNewTractament)) {
                    ModeAplicacio oldModeAplicacioIdOfTractamentCollectionNewTractament = tractamentCollectionNewTractament.getModeAplicacioId();
                    tractamentCollectionNewTractament.setModeAplicacioId(modeAplicacio);
                    tractamentCollectionNewTractament = em.merge(tractamentCollectionNewTractament);
                    if (oldModeAplicacioIdOfTractamentCollectionNewTractament != null && !oldModeAplicacioIdOfTractamentCollectionNewTractament.equals(modeAplicacio)) {
                        oldModeAplicacioIdOfTractamentCollectionNewTractament.getTractamentCollection().remove(tractamentCollectionNewTractament);
                        oldModeAplicacioIdOfTractamentCollectionNewTractament = em.merge(oldModeAplicacioIdOfTractamentCollectionNewTractament);
                    }
                }
            }
            for (Abonat abonatCollectionNewAbonat : abonatCollectionNew) {
                if (!abonatCollectionOld.contains(abonatCollectionNewAbonat)) {
                    ModeAplicacio oldModeAplicacioIdOfAbonatCollectionNewAbonat = abonatCollectionNewAbonat.getModeAplicacioId();
                    abonatCollectionNewAbonat.setModeAplicacioId(modeAplicacio);
                    abonatCollectionNewAbonat = em.merge(abonatCollectionNewAbonat);
                    if (oldModeAplicacioIdOfAbonatCollectionNewAbonat != null && !oldModeAplicacioIdOfAbonatCollectionNewAbonat.equals(modeAplicacio)) {
                        oldModeAplicacioIdOfAbonatCollectionNewAbonat.getAbonatCollection().remove(abonatCollectionNewAbonat);
                        oldModeAplicacioIdOfAbonatCollectionNewAbonat = em.merge(oldModeAplicacioIdOfAbonatCollectionNewAbonat);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = modeAplicacio.getId();
                if (findModeAplicacio(id) == null) {
                    throw new NonexistentEntityException("The modeAplicacio with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ModeAplicacio modeAplicacio;
            try {
                modeAplicacio = em.getReference(ModeAplicacio.class, id);
                modeAplicacio.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The modeAplicacio with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Tractament> tractamentCollectionOrphanCheck = modeAplicacio.getTractamentCollection();
            for (Tractament tractamentCollectionOrphanCheckTractament : tractamentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This ModeAplicacio (" + modeAplicacio + ") cannot be destroyed since the Tractament " + tractamentCollectionOrphanCheckTractament + " in its tractamentCollection field has a non-nullable modeAplicacioId field.");
            }
            Collection<Abonat> abonatCollectionOrphanCheck = modeAplicacio.getAbonatCollection();
            for (Abonat abonatCollectionOrphanCheckAbonat : abonatCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This ModeAplicacio (" + modeAplicacio + ") cannot be destroyed since the Abonat " + abonatCollectionOrphanCheckAbonat + " in its abonatCollection field has a non-nullable modeAplicacioId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(modeAplicacio);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ModeAplicacio> findModeAplicacioEntities() {
        return findModeAplicacioEntities(true, -1, -1);
    }

    public List<ModeAplicacio> findModeAplicacioEntities(int maxResults, int firstResult) {
        return findModeAplicacioEntities(false, maxResults, firstResult);
    }

    private List<ModeAplicacio> findModeAplicacioEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ModeAplicacio.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ModeAplicacio findModeAplicacio(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ModeAplicacio.class, id);
        } finally {
            em.close();
        }
    }

    public int getModeAplicacioCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ModeAplicacio> rt = cq.from(ModeAplicacio.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
